<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Information extends Api
{

    /**
     * Information模型对象
     * @var \app\admin\model\Information
     */
    protected $model = null;
    protected $modelLike = null;

    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Information;
        $this->modelLike = new \app\admin\model\information\Like;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 动态列表
     * @return void
     */
    public function index()
    {
        $params = $this->request->param();
        try {
            $where = [];
            if (!empty($params['type'])) {
                if ($params['type'] == 'my') {
                    $where['user_id'] = $this->auth->id;
                }
            }
            $list = $this->model->alias('m')->where($where)->with(['user'])->paginate($params['page_size'] ?? 10);

            foreach ($list as $item) {
                $item['createFlag'] = $item['user_id'] == $this->auth->id;
                $item['createtime_text'] = date('Y-m-d H:i:s', $item['createtime']);
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('查询成功', $list);
    }

    /**
     * 新增动态
     * @return void
     */
    public function add()
    {
        $params = $this->request->post();

        try {

            $params['user_id'] = $this->auth->id;

            //是否采用模型验证
            $validate_class = '\app\admin\validate\Information';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "add")) {
                throw new Exception($validate->getError());
            }

            $params['like_count'] = 0;
            $params['read_count'] = 0;
            $this->model->allowField(true)->save($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('新增成功');
    }

    /**
     * 编辑动态
     * @return void
     */
    public function edit()
    {
        $params = $this->request->post();
        try {
            //是否采用模型验证
            $validate_class = '\app\admin\validate\Information';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "edit")) {
                throw new Exception($validate->getError());
            }

            $this->model->allowField(true)->save($params, ['id' => $params['id']]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('修改成功');
    }

    /**
     * 删除动态
     * @return void
     */
    public function delete()
    {
        $params = $this->request->post();
        try {
            $validate_class = '\app\admin\validate\Information';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "delete")) {
                throw new Exception($validate->getError());
            }

            $this->model->destroy($params['id']);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功');
    }


    /**
     * 动态详情
     * @return void
     */
    public function info()
    {
        $params = $this->request->param();
        Db::startTrans();
        try {
            $validate_class = '\app\admin\validate\Information';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "delete")) {
                throw new Exception($validate->getError());
            }

            $data = $this->model->where('id', $params['id'])->find();
            $data['createFlag'] = $data['user_id'] == $this->auth->id;
            $data['createtime_text'] = date('Y-m-d H:i:s', $data['createtime']);
            $data['user'] = $this->auth->getUser();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $data);
    }

    /**
     * 动态点赞
     * @return void
     */
    public function like()
    {
        $params = $this->request->param();

        Db::startTrans();
        try {
            $validate_class = '\app\admin\validate\Information';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "delete")) {
                throw new Exception($validate->getError());
            }

            $where = [];
            $where['information_id'] = $params['id'];
            $where['user_id'] = $this->auth->id;
            $likeData = $this->modelLike->where($where)->find();

            if ($likeData) {
                if ($likeData->status == 1) {
                    $this->model->where('id', $params['id'])->setDec('like_count');
                    $likeData->save(['status' => 2]);
                } else {
                    $this->model->where('id', $params['id'])->setInc('like_count');
                    $likeData->save(['status' => 1]);
                }
            } else {
                $this->model->where('id', $params['id'])->setInc('like_count');

                $data = [
                    'user_id' => $this->auth->id,
                    'information_id' => $params['id'],
                    'status' => 1
                ];
                $this->modelLike->allowField(true)->save($data);
            }


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('点赞成功');
    }

    /**
     * 动态阅读
     * @return void
     */
    public function read()
    {
        $params = $this->request->post();

        try {
            if (empty($params['id'])) {
                throw new Exception('id不能为空');
            }
            $this->model->where('id', $params['id'])->setInc('read_count');

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('阅读成功');
    }

}
